package 关键字;

public class Example {

    /**
     * 多线程情况下
     * 指令重排序可能导致
     * writer的顺序打乱
     * ready=true但是number还没初始化
     * 再调用reader就会报错
     */
    public volatile boolean ready = false;

    public int number;

    public void writer(){
        number = 42;
        ready = true;
    }

    public void reader(){
        if(ready){
            int localNumber = number;
            System.out.println("number is:"+number);
        }
    }
}
